
package commonClasses.Messages;

import commonClasses.*;
import commonClasses.commonInterfaces.ItemIntf;
import commonClasses.commonInterfaces.MsgIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author Pozytywny!
 */
public class FinishMsg extends Msg implements MsgIntf {

    private ItemIntf _item;

    private long _itemID;
    private String _winnerHash;

    public long getItemID() {
        return _itemID;
    }

    public String getWinnerHash() {
        return _winnerHash;
    }

    public FinishMsg(ProtocolIntf protocol) {
        super(protocol, MagicNumbers.FINISH_MSG_NR);

    }

    public FinishMsg(ProtocolIntf protocol, ItemIntf item) {
        super(protocol, MagicNumbers.FINISH_MSG_NR);
        _item = item;
    }

    public void receive() throws Exception {
        _itemID = receive32uint();
        Utils.hostReceived(_itemID);

        _winnerHash = String.valueOf(receiveCharArr(MagicNumbers.HASH_LENGTH));
        Utils.hostReceived(_winnerHash);
    }

    public void send() throws Exception {

        int mesgId[] = this.idToIntArr();
        sendIntArr(mesgId);

        int itemIdArr[] = longToArr(_item.getId());
        sendIntArr(itemIdArr);

        char hash[] = _item.getWinnersHash().toCharArray();
        sendCharArr(hash);


    }

}
